<template >
    <el-upload class="upload-demo" ref="upload" action="" :http-request="()=>{}" v-bind="options" :before-upload="beforeAvatarUpload" :on-change="handleChange"
        :show-file-list="false">
        <el-button v-bind="options" v-on="options" ref="btn">{{options.label}}</el-button>
    </el-upload>
</template>

<script>
export default {
    props: {
        options: {
            default: () => {
                return {}
            },
            type: Object
        }
    },
    data () {
        return {
            resolve: null,
            reject: null,
        }
    },
    methods: {
        handlerClick (resolve, reject) {
            this.resolve = resolve;
            this.reject = reject;
            this.$refs.btn.$el.click();
        },
        beforeAvatarUpload (file) {
            let type = true;
            let msg = ''
            if (this.options.accept) {
                type = this.options.accept.indexOf(file.name.substring(file.name.lastIndexOf("."))) === -1 ? false : true;
            }
            if (!type) {
                msg = `只能上传${this.options.accept}格式!`
                this.$message.warning(msg);
                this.reject(msg)
                return false
            }
            const isLt = file.size / 1024 / 1024 < (this.options.fileSize || 1000);
            if (!isLt) {
                msg = `文件大小不能超过${this.options.fileSize}MB!`
                this.$message.warning(msg);
                this.reject(msg)
                return false
            }
        },
        handleChange (file, fileList) {
            if (this.options.multiple) {
                this.resolve(fileList)
            } else {
                this.resolve(file)
            }
        },
        clearFiles () {
            this.$refs.upload.clearFiles();
        }
    },

}
</script>